
void main() { 
var logger = Logger('UI');
logger.log('Button clicked');

var logMap = {'name': 'UI'};
var loggerJson = Logger.fromJson(logMap);

// 使用重定向工厂构造函数
  var mergedListenable = Listenable.merge([
    Listenable('Event 1'),
    Listenable('Event 2'),
  ]);

  mergedListenable.listen(); // 输出：Listening to merged events: [Event 1, Event 2]


 // 创建一个 Vector2d 实例
  var v2d = Vector2d.named(x: 1, y: 2);
  print('v2d: x = ${v2d.x}, y = ${v2d.y}'); // 输出: v2d: x = 1, y = 2

  // 创建一个 Vector3d 实例，使用 yzPlane 构造函数
  var v3dYZ = Vector3d.yzPlane(y: 3, z: 4);
  print('v3dYZ: x = ${v3dYZ.x}, y = ${v3dYZ.y}, z = ${v3dYZ.z}'); // 输出: v3dYZ: x = 0, y = 3, z = 4

  // 创建一个 Vector3d 实例，使用默认构造函数
  var v3d = Vector3d(x: 5, y: 6, z: 7);
  print('v3d: x = ${v3d.x}, y = ${v3d.y}, z = ${v3d.z}'); 
}

//https://dart.cn/language/constructors/#generative-constructors
class Point {
  // Instance variables to hold the coordinates of the point.
  double x;
  double y;

  // Generative constructor with initializing formal parameters:
  Point(this.x, this.y);
}


//https://dart.cn/language/constructors/#named-constructors
const double xOrigin = 0;
const double yOrigin = 0;

class Point1 {
  final double x;
  final double y;

  // Sets the x and y instance variables
  // before the constructor body runs.
  Point1(this.x, this.y);

  // Named constructor
  Point1.origin() : x = xOrigin, y = yOrigin;
}


//https://dart.cn/language/constructors/#constant-constructors

class ImmutablePoint {
  static const ImmutablePoint origin = ImmutablePoint(0, 0);

  final double x, y;

  const ImmutablePoint(this.x, this.y);
}




//https://dart.cn/language/constructors/#redirecting-constructors
class Point2 {
  double x, y;

  // The main constructor for this class.
  Point2(this.x, this.y);

  // Delegates to the main constructor.
  Point2.alongXAxis(double x) : this(x, 0);
}



//https://dart.cn/language/constructors/#factory-constructors
class Logger {
  final String name;
  bool mute = false;

  // _cache is library-private, thanks to
  // the _ in front of its name.
  static final Map<String, Logger> _cache = <String, Logger>{};

  factory Logger(String name) {
    return _cache.putIfAbsent(name, () => Logger._internal(name));
  }

  factory Logger.fromJson(Map<String, Object> json) {
    return Logger(json['name'].toString());
  }

  Logger._internal(this.name);

  void log(String msg) {
    if (!mute) print(msg);
  }
}


//https://dart.cn/language/constructors/#redirecting-factory-constructors

// 定义一个 Listenable 类
class Listenable {
  final String event;

  Listenable(this.event);

  void listen() {
    print('Listening to event: $event');
  }

  // 重定向工厂构造函数
  factory Listenable.merge(List<Listenable> listenables) = _MergingListenable;
}

// 定义另一个类 _MergingListenable
class _MergingListenable implements Listenable {
  final List<Listenable> listenables;

  _MergingListenable(this.listenables);

  @override
  String get event => 'Merged Events';

  @override
  void listen() {
    print('Listening to merged events: ${listenables.map((e) => e.event).toList()}');
  }
}


//https://dart.cn/language/constructors/#initialize-instance-variables-in-the-declaration
class PointA {
  double x = 1.0;
  double y = 2.0;

  // The implicit default constructor sets these variables to (1.0,2.0)
  // PointA();

  @override
  String toString() {
    return 'PointA($x,$y)';
  }
}


//https://dart.cn/language/constructors/#use-initializing-formal-parameters
class PointB {
  final double x;
  final double y;

  // Sets the x and y instance variables
  // before the constructor body runs.
  PointB(this.x, this.y);

  // Initializing formal parameters can also be optional.
  PointB.optional([this.x = 0.0, this.y = 0.0]);
}


class PointC {
  double x; // must be set in constructor
  double y; // must be set in constructor

  // Generative constructor with initializing formal parameters
  // with default values
  PointC.named({this.x = 1.0, this.y = 1.0});

  @override
  String toString() {
    return 'PointC.named($x,$y)';
  }
}

// Constructor using named variables.
final pointC = PointC.named(x: 2.0, y: 2.0);

class PointD {
  double? x; // null if not set in constructor
  double? y; // null if not set in constructor

  // Generative constructor with initializing formal parameters
  PointD(this.x, this.y);

  @override
  String toString() {
    return 'PointD($x,$y)';
  }
}


//https://dart.cn/language/constructors/#use-an-initializer-list
//https://dart.cn/language/constructors/#super-parameters
class Vector2d {
  final double x;
  final double y;

  // 命名构造函数
  Vector2d.named({required this.x, required this.y});

  // 默认构造函数
  Vector2d({this.x = 0, this.y = 0});
}

class Vector3d extends Vector2d {
  final double z;

  // 使用 super 参数将 y 参数传递给父类的命名构造函数
  Vector3d.yzPlane({required super.y, required this.z}) : super.named(x: 0);

  // 默认构造函数
  Vector3d({double x = 0, double y = 0, required this.z}) : super.named(x: x, y: y);
}
